// 位运算符测试用例
// 测试所有位运算符功能

func test_bitwise_basic_operations() {
    println("[SECTION A] 基本位运算符测试")
    
    var a int = 5
    var b int = 3
    
    // 按位与 (&)
    var bit_and int = a & b
    // 5 & 3 = 101 & 011 = 001 = 1
    assert(bit_and == 1, "按位与测试失败")
    println("A1. 按位与: 5 & 3 =", bit_and)
    
    // 按位或 (|)
    var bit_or int = a | b
    // 5 | 3 = 101 | 011 = 111 = 7
    assert(bit_or == 7, "按位或测试失败")
    println("A2. 按位或: 5 | 3 =", bit_or)
    
    // 按位异或 (^)
    var bit_xor int = a ^ b
    // 5 ^ 3 = 101 ^ 011 = 110 = 6
    assert(bit_xor == 6, "按位异或测试失败")
    println("A3. 按位异或: 5 ^ 3 =", bit_xor)
    
    // 左移 (<<)
    var left_shift int = a << 2
    // 5 << 2 = 101 << 2 = 10100 = 20
    assert(left_shift == 20, "左移测试失败")
    println("A4. 左移: 5 << 2 =", left_shift)
    
    // 右移 (>>)
    var right_shift int = a >> 1
    // 5 >> 1 = 101 >> 1 = 10 = 2
    assert(right_shift == 2, "右移测试失败")
    println("A5. 右移: 5 >> 1 =", right_shift)
    
    // 按位非 (~)
    var bit_not int = ~a
    // ~5 = ~00000000000000000000000000000101 = 11111111111111111111111111111010 = -6 (32位有符号整数)
    assert(bit_not == -6, "按位非测试失败")
    println("A6. 按位非: ~5 =", bit_not)
    
    println("[PASSED] 基本位运算符测试通过")
}

func test_bitwise_assignment_operations() {
    println("[SECTION B] 位运算符赋值测试")
    
    var c int = 8
    println("B1. 初始值: c =", c)
    
    c &= 3  // c = c & 3 = 8 & 3 = 1000 & 0011 = 0000 = 0
    assert(c == 0, "按位与赋值测试失败")
    println("B2. 按位与赋值: c &= 3, c =", c)
    
    c |= 5  // c = c | 5 = 0 | 5 = 0000 | 0101 = 0101 = 5
    assert(c == 5, "按位或赋值测试失败")
    println("B3. 按位或赋值: c |= 5, c =", c)
    
    c ^= 2  // c = c ^ 2 = 5 ^ 2 = 0101 ^ 0010 = 0111 = 7
    assert(c == 7, "按位异或赋值测试失败")
    println("B4. 按位异或赋值: c ^= 2, c =", c)
    
    c <<= 1 // c = c << 1 = 7 << 1 = 1110 = 14
    assert(c == 14, "左移赋值测试失败")
    println("B5. 左移赋值: c <<= 1, c =", c)
    
    c >>= 2 // c = c >> 2 = 14 >> 2 = 0011 = 3
    assert(c == 3, "右移赋值测试失败")
    println("B6. 右移赋值: c >>= 2, c =", c)
    
    println("[PASSED] 位运算符赋值测试通过")
}

func test_bitwise_complex_operations() {
    println("[SECTION C] 复杂位运算测试")
    
    var flags int = 0
    println("C1. 初始标志位: flags =", flags)
    
    flags |= 1 << 0  // 设置第0位
    flags |= 1 << 2  // 设置第2位
    flags |= 1 << 4  // 设置第4位
    // flags = 00010101 = 21
    assert(flags == 21, "标志位设置测试失败")
    println("C2. 设置标志位后: flags =", flags)
    
    var has_bit_0 bool = (flags & (1 << 0)) != 0  // true
    var has_bit_1 bool = (flags & (1 << 1)) != 0  // false
    var has_bit_2 bool = (flags & (1 << 2)) != 0  // true
    
    assert(has_bit_0 == true, "第0位检查失败")
    assert(has_bit_1 == false, "第1位检查失败")
    assert(has_bit_2 == true, "第2位检查失败")
    println("C3. 位检查: bit0=", has_bit_0, "bit1=", has_bit_1, "bit2=", has_bit_2)
    
    // 清除第2位
    flags &= ~(1 << 2)  // flags = 00010001 = 17
    assert(flags == 17, "清除第2位测试失败")
    println("C4. 清除第2位后: flags =", flags)
    
    // 切换第3位
    flags ^= 1 << 3    // flags = 00011001 = 25
    assert(flags == 25, "切换第3位测试失败")
    println("C5. 切换第3位后: flags =", flags)
    
    println("[PASSED] 复杂位运算测试通过")
}

func test_bitwise_comprehensive() {
    println("[SECTION D] 综合验证")
    
    var a int = 5
    var b int = 3
    var bit_and int = a & b
    var bit_or int = a | b
    var bit_xor int = a ^ b
    var left_shift int = a << 2
    var right_shift int = a >> 1
    var bit_not int = ~a
    var c int = 3
    var flags int = 25
    
    var result int = bit_and + bit_or + bit_xor + left_shift + right_shift + bit_not + c + flags
    // 预期结果: 1 + 7 + 6 + 20 + 2 + (-6) + 3 + 25 = 58
    var expected int = 58
    assert(result == expected, "综合验证失败")
    println("D1. 综合结果:", result, "(预期:", expected, ")")
    
    println("[PASSED] 综合验证通过")
}
